// 假设这是脏话列表，可根据需求扩展
const profanityWords = ["妈逼", "傻逼"];

const ProfanityFilterPlugin = {
    install: (app) => {
        app.directive("profanity-filter", {
            mounted: (el, binding, vnode) => {
                const handleInput = () => {
                    const value = el.value;
                    let cleanValue = value;
                    profanityWords.forEach((word) => {
                        const regex = new RegExp(word, "gi");
                        cleanValue = cleanValue.replace(regex, "***");
                    });
                    if (cleanValue !== value) {
                        el.value = cleanValue;
                        // 更新 v-model 绑定的值
                        const set = vnode.props["onUpdate:modelValue"];
                        if (set) {
                            set(cleanValue);
                        }
                    }
                };
                el.addEventListener("input", handleInput);
            },
            unmounted: (el) => {
                const handleInput = () => {};
                el.removeEventListener("input", handleInput);
            },
        });
    },
};

export default ProfanityFilterPlugin;
